package com.jxb.bojiaoMybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxb.importtest.BojiaoImport;
import com.jxb.mapper.UserMapper;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/11/23 20:36
 **/
public class BojiaoFactoryBean implements FactoryBean {

    private Class mapperInterface;

    private SqlSession sqlSession;

    public BojiaoFactoryBean(Class mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Autowired
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactory.getConfiguration().addMapper(mapperInterface);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public Object getObject() throws Exception {
        return sqlSession.getMapper(mapperInterface);
        // Object o = Proxy.newProxyInstance(BojiaoFactoryBean.class.getClassLoader(),
        //                                   new Class[] {UserMapper.class},
        //                                   new InvocationHandler() {
        //                                       @Override
        //                                       public Object invoke(Object proxy, Method method, Object[] args)
        //                                               throws Throwable {
        //                                           return null;
        //                                       }
        //                                   });
        // return o;
    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }
}
